<?php
class NHSChoicesWidgetController extends WP_Widget {
	function NHSChoicesWidgetController() {
		$widget_ops = array(
			'classname' => 'widget_nhs_choices',
			'description' => 'Add a list of NHS services'
		);
		
		$this->WP_Widget('nhs-choices', 'NHS Choices', $widget_ops);
	}
	
	function widget($args, $instance) {
		extract($args, EXTR_SKIP);
		echo $before_widget;
		$title = apply_filters('widget_title', $instance['title']);
		$service = intval($instance['service']);
		$location = $instance['location'];
		$rowcount = intval($instance['rowcount']);
		
		if($title) {
			echo $before_title . $title . $after_title;
		}
		
		global $nhs_choices;
		
		$api = $nhs_choices->api;
		$db = $nhs_choices->db;
		
		if($api->update_needed()) {
			$results = $api->get_results($service, $location, $rowcount);
			$db->add_array($results, $location);
		} else {
			$results = $db->get_results($service, $location, $rowcount);
		}
		
		if(count($results) > 0) {
			?>
			<ul>
				<?php foreach($results as $result) { ?>
					<li>
						<a href="<?php echo $result['url']; ?>"><?php echo htmlentities($result['name']); ?></a><br />
						<?php echo htmlentities($result['address3']); ?>
					</li>
				<?php } ?>
			</ul>
			<?php
		}
		
		echo $after_widget;
	}
	
	function update($new_instance, $old_instance) {
		$instance = $old_instance;
		$old_service = intval($instance['service']);
		$old_location = $instance['location'];
		$old_rowcount = intval($instance['rowcount']);
		
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['service'] = intval(strip_tags($new_instance['service']));
		$instance['location'] = strip_tags($new_instance['location']);
		$instance['display'] = strip_tags($new_instance['display']);
		$instance['googlemaps_api_key'] = strip_tags($new_instance['googlemaps_api_key']);
		$instance['rowcount'] = intval(strip_tags($new_instance['rowcount']));
		
		if($instance['service'] != $old_service || $instance['location'] != $old_location || $old_rowcount < $instance['rowcount']) {
			global $nhs_choices;
			
			$api = $nhs_choices->api;
			$api->clear_cache();
			
			$db = $nhs_choices->db;
			$db->clear_cache();
		}
		
		return $instance;
	}
	
	function form($instance) {
		global $NHS_SERVICES;
		$display_id = $this->get_field_id('display');
		$googlemaps_id = $this->get_field_id('googlemaps');
		
		$title = $instance['title'];
		$service = intval($instance['service']);
		$location = $instance['location'];
		$display = $instance['display'];
		$rowcount = $instance['rowcount'];
		$googlemaps_api_key = $instance['googlemaps_api_key'];
		
		?>
		
		<p><label>Title<br />
		<input class="widefat" type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $title; ?>" />
		</label></p>
		
		<p><label>Service<br /><select class="widefat" id="<?php echo $this->get_field_id('service'); ?>" name="<?php echo $this->get_field_name('service'); ?>"><?php
		
		foreach($NHS_SERVICES as $id => $label) {
			?><option value="<?php echo $id; ?>"<?php if(intval($id) == $service) echo ' selected="selected"'; ?>><?php echo $label; ?></option><?php
		}
		
		?></select></label></p>
		
		<p><label>Location (postcode or town name)<br />
		<input class="widefat" type="text" id="<?php echo $this->get_field_id('location'); ?>" name="<?php echo $this->get_field_name('location'); ?>" value="<?php echo $location; ?>" />
		</label></p>
		
		<p><label>Display<br />
		<select class="widefat" id="<?php echo $display_id; ?>" name="<?php echo $this->get_field_name('display'); ?>">
			<option value="l"<?php if($display == 'l') echo ' selected="selected"'; ?>>List</option>
			<option value="m"<?php if($display == 'm') echo ' selected="selected"'; ?>>Map</option>
		</select>
		</label></p>
		
		<p id="<?php echo $googlemaps_id; ?>"><label>Google Maps API key<br />
		<input class="widefat" type="text" id="<?php echo $this->get_field_id('googlemaps_api_key'); ?>" name="<?php echo $this->get_field_name('googlemaps_api_key'); ?>" value="<?php echo $googlemaps_api_key; ?>" />
		</label></p>
		
		<p><label>Services to display<br />
		<select class="widefat" id="<?php echo $this->get_field_id('rowcount'); ?>" name="<?php echo $this->get_field_name('rowcount'); ?>"><?php
			for($i = 1; $i <= 10; $i ++) {
				?><option value="<?php echo $i; ?>"<?php if($i == $rowcount) echo ' selected="selected"'; ?>><?php echo $i; ?></option><?php
			}
		?></select>
		</label></p>
		
		<script type="text/javascript">
			jQuery('#<?php echo $display_id; ?>').bind('click',
				function(e) {
					val = jQuery(this).val();
					if(val == 'm') {
						jQuery('#<?php echo $googlemaps_id; ?>').show();
					} else {
						jQuery('#<?php echo $googlemaps_id; ?>').hide();
					}
				}
			);
			
			jQuery(document).ready(
				function() {
					val = jQuery('#<?php echo $display_id; ?>').val();
					
					if(val == 'l') {
						jQuery('#<?php echo $googlemaps_id; ?>').hide();
					}
				}
			);
		</script>
		
		<?php
	}
}
?>